# -*- coding:utf-8 -*-

from multi_robot_problem import MultiRobotState
from time import time
from manhattanHeuristic import manhattenSumHuristics
from astar import AStar
from best_first import BestFirstGraphSearch
from graph import GraphSearch
from pile2robotHeuristic import pile2robot
from manAndRatioHeuristic import mixedWithManSum, mixed
from avoidPocketsHeuristic import avoidPockets
from Tester import Tester
from anytimeAlgorithms import AnytimeBestFirstGraphSearch
from anytimeAlgorithms import AnytimeAstarGraphSearch
from manAndNumOfPiles import ManAndPiles
from experimens import ExperimentRunner
from agent import Smith
from scipy.stats import wilcoxon

"""
Created on Apr 11, 2011

@author: bob
"""
#---------------The second pocket problem - the one with 72/69/79 results
#width = 10
#height = 10
#robots = ((9, 5), (8, 7), (7, 3))
#dirt_locations = frozenset([(9, 1), (2, 8), (7, 5), (1, 6), (4, 3), (3, 7), (2, 3), (8, 5), (5, 8), (3, 5)])
#obstacle_locations = frozenset([(6, 4), (3, 2), (0, 0), (6, 6), (3, 0), (8, 0), (6, 0), (1, 4), (5, 9), (6, 2), (7, 0), (1, 0), (6, 8), (7, 4), (6, 9), (6, 7), (4, 6), (2, 0), (6, 1), (3, 1), (6, 3), (5, 6), (3, 9), (3, 6), (3, 3), (4, 9), (3, 4), (2, 4), (8, 4)])
#problem = MultiRobotState(width, height, robots, dirt_locations, obstacle_locations)

#----------------First pocket problem
#width = 10
#height = 10
#robots = ((0, 6), (2, 6), (5, 3))
#dirt_locations = frozenset([(9, 0), (8, 7), (0, 9), (6, 1), (3, 2), (4, 7)])
#obstacle_locations = frozenset([(4, 4), (6, 7), (4, 6), (7, 1), (4, 5), (2, 8), (5, 7), (7, 7), (3, 8), (4, 3), (2, 2), (4, 8), (5, 8)])
#problem = MultiRobotState(width, height, robots, dirt_locations, obstacle_locations)
#print problem 

#----------------Proble that fucked up the ratio heuristic
#width = 13
#height = 12
#robots = ((6, 11), (7, 11), (3, 10))
#dirt_locations = frozenset([(12, 2), (11, 10), (0, 0), (4, 9), (12, 0), (11, 2), (3, 2), (11, 4), (2, 3), (1, 9), (9, 5), (10, 2), (8, 5), (2, 4), (4, 0)])
#obstacle_locations = frozenset([(10, 11), (4, 8), (2, 8), (8, 11), (8, 9), (10, 1), (5, 8), (6, 8), (10, 10), (9, 2), (4, 4), (8, 10), (3, 8), (1, 8), (11, 1), (1, 10), (9, 11), (10, 9)])
#problem = MultiRobotState(width, height, robots, dirt_locations, obstacle_locations)

#----------------Again Proble that fucked up the ratio heuristic
#width = 12
#height = 11
#robots = ((8, 0), (1, 2), (11, 9))
#dirt_locations = frozenset([(5, 5), (10, 10), (8, 1), (5, 7), (2, 10), (4, 8), (2, 0), (1, 8), (8, 8), (4, 3), (10, 7), (6, 2), (10, 0), (5, 8), (3, 5)])
#obstacle_locations = frozenset([(10, 5), (0, 0), (6, 7), (11, 5), (5, 6), (7, 6), (4, 4), (7, 7), (8, 7), (9, 7)])
#problem = MultiRobotState(width, height, robots, dirt_locations, obstacle_locations)

#----------------Check nops and delta: in the heuristic - delta/6 is better that delta/2
#width = 18
#height = 18
#robots = ((4, 17), (5, 13), (5, 8))
#dirt_locations = frozenset([(10, 17), (13, 15), (2, 1), (15, 10), (3, 12), (14, 16), (1, 2), (17, 6), (3, 3), (2, 9), (1, 16), (16, 8), (13, 16), (2, 4), (6, 3), (15, 11), (2, 13), (9, 17), (16, 15), (14, 14), (3, 16), (11, 15), (16, 13), (1, 1), (8, 15)])
#obstacle_locations = frozenset([(7, 3), (10, 11), (16, 6), (7, 12), (0, 14), (10, 6), (7, 7), (12, 5), (0, 10), (7, 11), (15, 5), (7, 2), (15, 12), (10, 8), (13, 12), (14, 5), (11, 5), (10, 7), (7, 6), (10, 12), (0, 11), (16, 7), (7, 10), (12, 10), (17, 14), (10, 9), (12, 12), (7, 1), (7, 5), (1, 9), (11, 12), (7, 9), (10, 5), (16, 5), (0, 15), (10, 10), (8, 12), (0, 13), (13, 5), (7, 4), (14, 12), (1, 8), (1, 7), (9, 12)])
#problem = MultiRobotState(width, height, robots, dirt_locations, obstacle_locations)

#----------------Check nops and delta: in the heuristic - delta/2 is better that delta/6
#width = 16
#height = 16
#robots = ((15, 12), (2, 5), (11, 6))
#dirt_locations = frozenset([(7, 3), (5, 4), (10, 4), (5, 6), (1, 4), (12, 5), (3, 9), (3, 7), (4, 11), (8, 14), (10, 5), (5, 10), (3, 3), (10, 10), (12, 13), (5, 2), (4, 4), (10, 12), (4, 13), (0, 6), (8, 15), (1, 10), (6, 15), (10, 9), (13, 11)])
#obstacle_locations = frozenset([(8, 7), (7, 12), (9, 8), (10, 6), (7, 7), (7, 11), (1, 11), (8, 5), (6, 7), (10, 7), (12, 9), (14, 1), (0, 11), (9, 11), (3, 6), (8, 6), (8, 11), (9, 7), (5, 11), (6, 11), (12, 7), (9, 10), (7, 5), (12, 11), (12, 10), (9, 6), (2, 7), (6, 8), (7, 13), (2, 11), (9, 9), (8, 8), (9, 5), (7, 8), (12, 8)])
#problem = MultiRobotState(width, height, robots, dirt_locations, obstacle_locations)

#def __init__(self, minWidth, maxWidth, minHeight, maxHeight, maxRobots, maxPiles, maxObstacles, \
#        obstacleType, wallsLenght, wallsVariance, pocketWidth, \
#        pocketWidthVariance, pocketHeight, pocketHeightVariance, minPockets = 0, minWalls = 0)

#------------------isSolvableProblem bug 
#width = 15
#height = 15
#robots = ((7, 3), (5, 2), (4, 6), (4, 11))
#dirt_locations = frozenset([(4, 3), (1, 3), (13, 13), (14, 4), (11, 9), (6, 13), (11, 0), (3, 9), (1, 6), (3, 7), (7, 11), (10, 4), (7, 0), (4, 9), (1, 12), (8, 1), (4, 4), (1, 8), (14, 12), (0, 11), (3, 6), (8, 6), (3, 5), (11, 6), (13, 11)])
#obstacle_locations = frozenset([(7, 14), (14, 10), (9, 14), (4, 5), (0, 7), (9, 10), (4, 12), (0, 5), (2, 12), (6, 12), (0, 8), (13, 10), (6, 14), (9, 13), (11, 10), (3, 12), (10, 10), (5, 12), (11, 5), (9, 9), (5, 14), (0, 6), (9, 11), (0, 9), (12, 10), (9, 12)])
#problem = MultiRobotState(width, height, robots, dirt_locations, obstacle_locations)
#
#t = Tester(12, 15, 12, 15, 4, 25, 8, Tester.OBSTACLES_ALL, 4,2,4,2,1,1)
#t.printProblemSpaceParams(problem)
#print problem
#if t.isSolvableProblem(problem):
#    print 1
#else:
#    print 2
#exit(1)

#width = 10
#height = 5
#robots = ((3, 2), (4, 4), (1, 0))
#dirt_locations = frozenset([(1, 2), (9, 0), (3, 3), (3, 0), (3, 4), (3, 1), (6, 0), (2, 1), (6, 2), (5, 3), (9, 4), (5, 1), (8, 4), (4, 1), (4, 0)])
#obstacle_locations = frozenset([(8, 3), (1, 3), (0, 2), (7, 2)])
#problem = MultiRobotState(width, height, robots, dirt_locations, obstacle_locations)

#width = 13
#height = 15
#robots = ((4, 2), (11, 10), (4, 14), (5, 0))
#dirt_locations = frozenset([(7, 3), (4, 7), (12, 1), (9, 13), (2, 8), (8, 0), (5, 4), (2, 10), (12, 5), (6, 2), (0, 5), (1, 9), (9, 4), (6, 14), (0, 1), (11, 7), (10, 5), (5, 5), (9, 2), (6, 10), (8, 8), (9, 11), (7, 10), (5, 3), (3, 4)])
#obstacle_locations = frozenset([(2, 6), (6, 6), (5, 6), (8, 9), (1, 6), (10, 1), (3, 7), (7, 9), (7, 6), (9, 9), (3, 8), (10, 14), (3, 6), (0, 4), (10, 0), (3, 5), (10, 9), (10, 2)])
#problem = MultiRobotState(width, height, robots, dirt_locations, obstacle_locations)
#
#width = 15
#height = 10
#robots = ((4, 2), (6, 7), (7, 2), (0, 7))
#dirt_locations = frozenset([(6, 9), (0, 0), (12, 1), (4, 3), (6, 6), (4, 5), (10, 6), (12, 5), (1, 6), (1, 9), (3, 7), (0, 3), (8, 5), (6, 5), (3, 5), (13, 6), (13, 1), (9, 2), (6, 1), (4, 4), (13, 5), (1, 8), (12, 4), (5, 0), (2, 2)])
#obstacle_locations = frozenset([(6, 4), (5, 4), (8, 2), (7, 1), (11, 9), (0, 5), (13, 8), (10, 8), (8, 3), (6, 3), (0, 6), (8, 1), (12, 6), (7, 4), (11, 8), (2, 3), (13, 9), (12, 8), (14, 8), (8, 4)])
#problem = MultiRobotState(width, height, robots, dirt_locations, obstacle_locations)

#e = ExperimentRunner()
#results = e.phaseResults()
#for r in results:
#    print r[e.RES_NAME] 
#print wilcoxon(results[1][e.RES_LIST],results[0][e.RES_LIST])
#exit(1)
#e.anytimebfTimes()

#e.mediumProblems()
#e.outerDeltaExpiriment()


#for i in xrange(0,10):
t = Tester(5, 5, 12, 15, 3, 25, 3, Tester.OBSTACLES_ALL, 4,2,4,2,1,1)
problem = t.generateProblemSpace()
t.printProblemSpaceParams(problem)
print problem
obj = Smith().solve(problem, 40)
if obj is None:
    print "Solution not found..."
else:
    print obj
    print len(obj)

exit(1)


print "---Best first: manhattan solution---"
algo = BestFirstGraphSearch()
start = time()
obj = algo.find(problem, ManAndPiles())
end = time()
print end-start
if obj is None:
    print "Solution not found..."
else:
    print obj
    print len(obj)

print "---Best first: Mixed solution---"
algo = AnytimeBestFirstGraphSearch()
start = time()
obj = algo.find(problem, mixed(), None)
end = time()
print end-start
if obj is None:
    print "Solution not found..."
else:
    print obj
    print len(obj)

print "---Best first: Mixed + man solution---"
algo = AnytimeBestFirstGraphSearch()
start = time()
obj = algo.find(problem, mixedWithManSum(0.9), None)
end = time()
print end-start
if obj is None:
    print "Solution not found..."
else:
    print obj
    print len(obj)

print "---A* anytime: mixed solution---"
algo = AnytimeAstarGraphSearch()
start = time()
obj = algo.find(problem, mixedWithManSum(0.9), 40, 0.6)
print end-start  
if obj is None:
    print "Solution not found..."
else:
    print obj
    print len(obj)

print "---Best first anyTime: mixed with man solution---"
BestFirst = AnytimeBestFirstGraphSearch()
start = time()
obj = BestFirst.find(problem, mixedWithManSum(0.9), 30)
end = time()
print end-start
if obj is None:
    print "Solution not found..."
else:
    print obj
    print len(obj)
    
print "---Best first anyTime: mixed solution---"
BestFirst = AnytimeBestFirstGraphSearch()
start = time()
obj = BestFirst.find(problem, mixed(), 30)
end = time()
print end-start
if obj is None:
    print "Solution not found..."
else:
    print obj
    print len(obj)
    
print "---A*: man solution---"
algo = AStar()
start = time()
obj = algo.find(problem, ManAndPiles())
end = time()
print end-start  
print obj
print len(obj)

print "---A*: mixed + man solution---"
algo = AStar()
start = time()
obj = algo.find(problem, mixedWithManSum(0.9))
end = time()
print end-start  
print obj
print len(obj)